<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="物品名称/品牌/规格/描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="categoryId">
        <treeselect
          style="width: 150px"
          v-model="queryParams.categoryId"
          :options="categoryOptions"
          :show-count="true"
          placeholder="请选择分类"
        />
      </el-form-item>
      <el-form-item label="仓库" prop="storeId">
        <el-select size="medium" v-model="queryParams.storeId" clearable>
          <el-option
            v-for="store in storeList"
            :key="store.id"
            :label="store.storeName"
            :value="store.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          round
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['goods:goods:add']"
          >添加物品</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button type="primary" round @click="handleStore"
          >仓库管理</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" round @click="handleCategory"
          >分类管理</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="goodsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="物品" align="center" prop="goodsCover">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.goodsCover"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="物品名称" align="center" prop="goodsName" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="分类" align="center" prop="categoryName" />
      <el-table-column label="数量" align="center" prop="amounts" />
      <el-table-column label="规格" align="center" prop="spec" />
      <el-table-column label="描述" align="center" prop="goodsDesc" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['goods:goods:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="getStorkRecord(scope.row.id)"
            >出入库明细
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="120px"
        heigh="20px"
      >
        <el-form-item label="物品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入物品名称" />
        </el-form-item>
        <el-form-item label="分类" prop="categoryId">
          <treeselect
            style="width: 150px"
            v-model="form.categoryId"
            :options="categoryOptions"
            :show-count="true"
            placeholder="请选择分类"
          />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌(不大于50字)" />
        </el-form-item>
        <el-form-item label="图片:" prop="goodsCover">
          <el-upload
            ref="upfile"
            action="upload"
            style="display: inline"
            :auto-upload="false"
            :on-change="handleChange"
            :file-list="fileList"
          >
            <el-button type="primary"
              >上传<i class="el-icon-upload el-icon--right"></i
            ></el-button>
          </el-upload>
        </el-form-item>

        <el-form-item label="规格" prop="spec">
          <el-input
            type="textarea"
            v-model="form.spec"
            placeholder="请输入规格(不大于50字)"
          />
        </el-form-item>
        <el-form-item label="描述" prop="goodsDesc">
          <el-input
            type="textarea"
            v-model="form.goodsDesc"
            placeholder="请输入描述(不大于200字)"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="出入库明细"
      :visible.sync="recordDialog.open"
      width="1200px"
      append-to-body
    >
      <el-table
        ref="tables"
        v-loading="recordDialog.loading"
        :data="recordDialog.list"
      >
        <el-table-column label="序号" align="center" prop="id" />
        <el-table-column label="类型" align="center" prop="type" width="150">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.bus_stock_status"
              :value="scope.row.type"
            ></dict-tag>
          </template>
        </el-table-column>

        <el-table-column
          label="操作时间"
          align="center"
          prop="operateDate"
          width="150"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="物品"
          align="center"
          prop="goodsName"
          width="150"
        />
        <el-table-column label="品牌" align="center" prop="brand" width="150" />
        <el-table-column
          label="仓库"
          align="center"
          prop="storeName"
          width="150"
        />
        <el-table-column
          label="数量"
          align="center"
          prop="amounts"
          width="150"
        />
        <el-table-column label="单价" align="center" prop="price" width="150" />
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button @click="closerecordDialog">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGoods,
  getGoods,
  delGoods,
  addGoods,
  updateGoods,
  listStore,
  listStockRecord,
} from "@/api/business/goods";
import { treeselect } from "@/api/business/category";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Goods",
  components: { Treeselect },
  dicts: ["bus_stock_status"],
  data() {
    return {
      // 审批历史弹窗
      recordDialog: {
        open: false,
        loading: false,
        list: [],
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物品信息表格数据
      goodsList: [],
      // 部门树选项
      categoryOptions: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //文件上传数组
      fileList: [],
      //仓库列表
      storeList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: null,
        categoryId: null,
        storeId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsName: [
          { required: true, message: "物品名称不能为空", trigger: "blur" },
        ],
        categoryId: [
          { required: true, message: "分类不能为空", trigger: "blur" },
        ],
        brand: [{ required: true, message: "品牌不能为空", trigger: "blur" }],
        spec: [
          { required: true, message: "物品规格不能为空", trigger: "blur" },
        ],
        goodsDesc: [
          { required: true, message: "物品描述不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getStore();
  },
  methods: {
    /** 查询物品信息列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then((response) => {
        this.goodsList = response.rows;
        console.log(this.goodsList.goodsCover);
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询物品分类下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.categoryOptions = response.data;
      });
    },
    //获取仓库列表
    getStore() {
      listStore().then((response) => {
        this.storeList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsName: null,
        goodsCover: null,
        categoryId: null,
        brand: null,
        spec: null,
        goodsDesc: null,
      };
      this.fileList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 文件变化操作 */
    handleChange(file, fileList) {
      this.fileList = fileList;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getGoods(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物品信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            //提交表单数据,文件上传规定参数
            let formData = new FormData();
            formData.append("id", this.form.id);
            if (this.fileList.length > 0) {
              console.log(this.fileList);
              formData.append("goodsCover", this.fileList[0].raw);
            }
            formData.append("goodsName", this.form.goodsName);
            formData.append("categoryId", this.form.categoryId);
            formData.append("brand", this.form.brand);
            formData.append("spec", this.form.spec);
            formData.append("goodsDesc", this.form.goodsDesc);
            updateGoods(formData).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            let formData = new FormData();
            formData.append("goodsCover", this.fileList[0].raw);
            formData.append("goodsName", this.form.goodsName);
            formData.append("categoryId", this.form.categoryId);
            formData.append("brand", this.form.brand);
            formData.append("spec", this.form.spec);
            formData.append("goodsDesc", this.form.goodsDesc);
            addGoods(formData).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除物品信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delGoods(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    //跳转到分类列表
    handleCategory() {
      this.$router.push({
        path: "/business/category",
      });
    },
    handleStore() {
      this.$router.push({
        path: "/business/store",
      });
    },
    getStorkRecord(id) {
      this.recordDialog.open = true;
      this.recordDialog.loading = true;
      // 请求审批历史接口的方法
      listStockRecord(id).then((res) => {
        this.recordDialog.list = res.data;
        this.recordDialog.loading = false;
      });
    },
    closerecordDialog() {
      this.recordDialog.open = false;
    },
  },
};
</script>
